// pages/evaluate/evaluateList/evaluateList.js
var http = require('../../../utils/server.js');
var util = require('../../../utils/util.js');
Page({
  data: {
    tags: [],
    page: 1,
    is_over: false,
    data: []
  },

  onLoad: function(option) {
    var that = this
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          height: res.windowHeight
        })
      }
    })
    that.setData({
      user_id: option.user_id || ""
    })

    http.post({
      url: "shop/getTags",
      user_id: that.data.user_id
    }, function(data) {
      console.log('tags', data)
      var tags = [{
        name: '全部',
        value: -1
      }, {
        name: '带图点评',
        value: 0
      }]
      var configTags = data.config_tags
      for (i in data.tags) {
        tags.push({
          name: configTags[i],
          value: i,
          num: data.tags[i]
        })
      }
      that.setData({
        tags: tags
      })
    })
    that.getData()
  },
  getData: function(e) {
    var that = this
    if (that.data.is_over)
      return
    util.hideToast()
    util.showBusy('努力加载中…')
    var tags = that.data.tags
    selected = -1
    for (i of tags) {
      if (i.selected)
        selected = i.value
    }
    http.post({
      url: "shop/getEvaluateList",
      user_id: that.data.user_id,
      page: that.data.page,
      tags: selected
    }, function(data) {
      console.log('评论列表', data)
      util.hideToast()
      if (data.is_over) {
        that.setData({
          is_over: true
        })
        util.showSuccess('已全部加载')
      }
      var tdata = that.data.data
      var newData = tdata.concat(data.data)
      var page = that.data.page + 1
      that.setData({
        data: newData,
        page: page
      })

    })

  },
  selectTag: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var tags = that.data.tags
    for (i of tags) {
      i.selected = false
      if (i.value == index) {
        i.selected = true
      }
    }
    that.setData({
      tags: tags,
      page: 0,
      data:[],
      is_over:false
    })
    that.getData()

  },
  preview(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var pindex = e.currentTarget.dataset.pindex
    var urls = []
    var data = that.data.data
    for (i of data[pindex].pics) {
      urls.push(i)
    }
    wx.previewImage({
      current: urls[index],
      urls: urls
    })
  },
})